#include "stdafx.h"
#include "Arch_Tools.h"

USING_NAMESPACE_PBBIM
USING_NAMESPACE_BENTLEY_SQLITE
USING_NAMESPACE_BENTLEY_SQLITE_EC
USING_NAMESPACE_EC
USING_NAMESPACE_BENTLEY_DGNPLATFORM
USING_NAMESPACE_BENTLEY_MOBILEDGN
USING_NAMESPACE_PBBIM_CORE
USING_NAMESPACE_PBBIM_COREMODEL
USING_NAMESPACE_PBBIM_ARCHMODEL

/*---------------------------------------------------------------------------------**//**
* @bsimethod                                    %Author%                       %CreateTime%
+---------------+---------------+---------------+---------------+---------------+------*/

PlaceArch%Class%Tool::PlaceArch%Class%Tool(): DgnPrimitiveTool(0)
{
}

/*---------------------------------------------------------------------------------**//**
* @bsimethod                                    %Author%                       %CreateTime%
+---------------+---------------+---------------+---------------+---------------+------*/
PlaceArch%Class%Tool::~PlaceArch%Class%Tool()
{
}

void PlaceArch%Class%Tool::_OnPostInstall()
{
	// new element : _OnPostInstall
    T_Super::_OnPostInstall();

    AccuDraw::GetInstance().Deactivate();
    AccuSnap::GetInstance().EnableLocate(false);
    AccuSnap::GetInstance().EnableSnap(true);

    MobileDgnApplication::App().Messages().Send(JsonMessage(PBBim_MESSAGE_ToolPrompt, "Input start point"));

    //_BeginDynamics();
}

void PlaceArch%Class%Tool::_OnRestartTool()
{
    PlaceArch%Class%Tool* newTool = new PlaceArch%Class%Tool();
    newTool->InstallTool();
}

bool PlaceArch%Class%Tool::_OnDataButton(DgnButtonEventCP ev)
{
	// new element : _OnDataButton
    m_points.push_back(*ev->GetPoint());

    if (1 <= m_points.size())
    {
        _BeginDynamics();
    }
    MobileDgnApplication::App().Messages().Send(JsonMessage(PBBim_MESSAGE_ToolPrompt, "Input next point"));

    return true;
}

bool PlaceArch%Class%Tool::_OnResetButton(DgnButtonEventCP)
{
	// new element : _OnResetButton
    DPoint3d end = m_points.at(0);
    m_points.push_back(end);
    PBArch%Class% m_%Class%;

    for (int i = 0; i < m_points.size() - 1; i++)
    {
        DSegment3d segment;
        segment.Init(m_points[i], m_points[i + 1]);
        ICurvePrimitivePtr line = ICurvePrimitive::CreateLine(segment);
        m_%Class%.AddProfileCurve((*line));
    }

    m_%Class%.SetThickness(0.2);
    DgnProjectPtr project = MobileDgnApplication::App().Project();
    LevelId levelId = LevelUtil::GetLevelIdByName(*project, LEVEL_ARCH_%ClassUp%);
    DgnModelId archModelId = ModelUtil::GetArchitectureModelId(*project);
    DgnModelP archModel = project->Models().GetModelById(archModelId);
    if (NULL == archModel)
        return NULL;

    ElementId elemId;

    PBDesignContext designContext;
    designContext.SetModel(archModel);
    designContext.SetLevelId(levelId);

    if (SUCCESS != m_%Class%.AddOrReplaceInModel(elemId, designContext))
        return false;
    _EndDynamics();
    _CloseCurrentTxn();

    _ExitTool();

    return true;
}

void PlaceArch%Class%Tool::_OnDynamicFrame(DgnButtonEventCP ev)
{
	// new element : _OnDynamicFrame
    if (NULL == ev)
        return;

    if (m_points.size() < 1)
        return;

    m_points.push_back(*ev->GetPoint());

    DgnProjectPtr project = MobileDgnApplication::App().Project();
    DgnModelId modelId = ModelUtil::GetDefaultModelId(*project);
    DgnModelP dgnModel = project->Models().GetModelById(modelId);
    if (NULL == dgnModel)
        return ;
    PhysicalGraphicsPtr %ClassDown%Graphics = dgnModel->CreatePhysicalGraphics();
    if (!%ClassDown%Graphics.IsValid())
    {
        m_points.pop_back();
        return;
    }
    ICurvePrimitivePtr curve = ICurvePrimitive::CreateLineString(m_points);
    CurveVectorPtr path = CurveVector::Create(curve);
    %ClassDown%Graphics->AddCurveVector(*path);
    if (%ClassDown%Graphics == NULL || BSISUCCESS != %ClassDown%Graphics->Finish())
        return;

    RedrawElems redrawElems;
    redrawElems.SetDrawMode(DgnDrawMode::TempDraw);
    redrawElems.SetDrawPurpose(DrawPurpose::Dynamics);
    redrawElems.SetDynamicsViews(IViewManager::GetActiveViewSet(), ev->GetViewport());
    redrawElems.DoRedraw(%ClassDown%Graphics->GetElementHandleR());
    m_points.pop_back();
}

void PlaceArch%Class%Tool::_ExitTool()
{
    SelectionSetManager::GetManager().EmptyAll();
    T_Super::_ExitTool();
}
